<?php
if(class_exists('SQL_Helper') != true)
{
   include_once("sql_helper.php");
}
include_once("sql_helper.php");
include_once("match_repository.php");
include_once("match.php");
include_once("doubles_repository.php");
include_once("doubles.php");
include_once("team_repository.php");
include_once("round_schedule_repository.php");
include_once("player_repository.php");
class Calculations
{
function calcPlayerScore($matchID){
	$score= SQL_Helper::executeQuery("SELECT * FROM  `game` INNER JOIN  `match` ON  `game`.matchID =  `match`.matchID WHERE  `game`.`matchID` =? ",$matchID);
	$player1TotalScore=0;
	$player2TotalScore=0;
	foreach ($score as $s){
		$player1TotalScore=$player1TotalScore+$s['player1Score'];
		$player2TotalScore=$player2TotalScore+$s['player2Score'];
	}
	$playersTotalScore = array();
	$playersTotalScore['player1TotalScore']=$player1TotalScore;
	$playersTotalScore['player2TotalScore']=$player2TotalScore;
	return $playersTotalScore;	
}
function wonGamesByMatch($matchID){
	$score= SQL_Helper::executeQuery("SELECT * FROM  `game` INNER JOIN  `match` ON  `game`.matchID =  `match`.matchID WHERE  `game`.`matchID` =? ",$matchID);
	$player1Games=0;
	$player2Games=0;
	$player1Games_lost=0;
	$player2Games_lost=0;
	foreach ($score as $s){
		if ($s['player1Score']>$s['player2Score']){
			$player1Games=$player1Games+1;
			$player2Games_lost=$player2Games_lost+1;}
		else if ($s['player1Score']<$s['player2Score']){
			$player2Games=$player2Games+1;
			$player1Games_lost=$player1Games_lost+1;}
	}
	$playersGames = array();
	$playersGames['player1Games']=$player1Games;
	$playersGames['player2Games']=$player2Games;
	$playersGames['player1Games_lost']=$player1Games_lost;
	$playersGames['player2Games_lost']=$player2Games_lost;
	return $playersGames;
}
function wonMatch($matchID){
	$playersGames=Calculations::wonGamesByMatch($matchID);
	$player1Match=0;
	$player2Match=0;
	$player1Match_lost=0;
	$player2Match_lost=0;
	if ($playersGames['player1Games']>$playersGames['player2Games']){
		$player1Match=1;
		$player2Match_lost=1;
	}
	if ($playersGames['player2Games']>$playersGames['player1Games']){
		$player2Match=1;
		$player1Match_lost=1;
	}
		
	
	$playersMatch = array();
	$playersMatch['player1Match']=$player1Match;
	$playersMatch['player2Match']=$player2Match;
	$playersMatch['player1Match_lost']=$player1Match_lost;
	$playersMatch['player2Match_lost']=$player2Match_lost;
	
	return $playersMatch;
}
function countWonMatches($round_scheduleID, $playerID){
	$repMatches=new MatchRepository;
	$matchs=$repMatches->maps($repMatches->getMatchByRoundScheduleIDandPlayerID($round_scheduleID,$playerID));
	$wonMatches=0;
	$lostMatches=0;
	foreach ($matchs as $m)
	{
		
		if ($m->player1ID==$playerID)
		{
			$playersMatch=Calculations::wonMatch($m->matchID);
			$playersMatch['player1Match'];
			$wonMatches=$wonMatches+$playersMatch['player1Match'];
			$lostMatches=$lostMatches+$playersMatch['player1Match_lost'];
		}
		if ($m->player2ID==$playerID)
		{
			$playersMatch=Calculations::wonMatch($m->matchID);
			$wonMatches=$wonMatches+$playersMatch['player2Match'];
			$lostMatches=$lostMatches+$playersMatch['player2Match_lost'];
		}
		
	}
	$winLostMatches= array();
	$winLostMatches['win']=$wonMatches;
	$winLostMatches['lost']=$lostMatches;
	return $winLostMatches;
}
function countTeamWonMatches($round_scheduleID){
	$repMatches=new MatchRepository;
	$matchs=$repMatches->maps($repMatches->getMatchByRoundScheduleID($round_scheduleID));
	$wonMatchesTeam1=0;
	$wonMatchesTeam2=0;
	$lostMatchesTeam1=0;
	$lostMatchesTeam2=0;
	foreach ($matchs as $m)
	{
		//echo $m->matchID."|";
			$playersMatch=Calculations::wonMatch($m->matchID);
			//$playersMatch['player1Match'];
			//print_r($playersMatch);
			$wonMatchesTeam1=$wonMatchesTeam1+$playersMatch['player1Match'];
			$wonMatchesTeam2=$wonMatchesTeam2+$playersMatch['player2Match'];
			$lostMatchesTeam1=$lostMatchesTeam1+$playersMatch['player1Match_lost'];
			$lostMatchesTeam2=$lostMatchesTeam2+$playersMatch['player2Match_lost'];
	}
	
	$team1WonDoubles=0;
	$team1LostDoubles=0;
	$team2WonDoubles=0;
	$team2LostDoubles=0;
	$doublesMatches=Calculations::countWonMatchesDoubles($round_scheduleID);
	
	$winLostTeamMatches= array();
	$winLostTeamMatches['winTeam1']=$wonMatchesTeam1+$doublesMatches['team1WonDoubles'];
	$winLostTeamMatches['lostTeam1']=$lostMatchesTeam1+$doublesMatches['team1LostDoubles'];
	$winLostTeamMatches['winTeam2']=$wonMatchesTeam2+$doublesMatches['team2WonDoubles'];
	$winLostTeamMatches['lostTeam2']=$lostMatchesTeam2+$doublesMatches['team2LostDoubles'];
	//print_r($winLostTeamMatches);
	return $winLostTeamMatches;
}
function countDoublesPoints($round_scheduleID){
	$repDoubles = new DoublesRepository;
	$doubles=$repDoubles->maps($repDoubles->getDoublesByScheduleID($round_scheduleID));
	$team1Points_doubles;
	$team2Points_doubles;
	foreach ($doubles as $d)
	{
		$team1Points_doubles=$team1Points_doubles+$d->team1Points;
		$team2Points_doubles=$team2Points_doubles+$d->team2Points;
	}
	return array($team1Points_doubles,$team2Points_doubles);
}

function countWonGamesDoubles($round_scheduleID){
	$repDoubles = new DoublesRepository;
	$doubles=$repDoubles->maps($repDoubles->getDoublesByScheduleID($round_scheduleID));
	$team1GamesWon_doubles=0;
	$team2GamesWon_doubles=0;
	$team1GamesLost_doubles=0;
	$team2GamesLost_doubles=0;
	foreach ($doubles as $d)
	{
		if ($d->team1Points>$d->team2Points){
			$team1GamesWon_doubles=$team1GamesWon_doubles+1;
		}
		else {
			$team1GamesLost_doubles=$team1GamesLost_doubles+1;
		}
		
		if ($d->team2Points>$d->team1Points){
			$team2GamesWon_doubles=$team2GamesWon_doubles+1;
		}
		else {
			$team2GamesLost_doubles=$team2GamesLost_doubles+1;
		}
		
	}
	return array('team1GamesWon_doubles'=>$team1GamesWon_doubles, 'team1GamesLost_doubles'=>$team1GamesLost_doubles, 'team2GamesWon_doubles'=>$team2GamesWon_doubles,'team2GamesLost_doubles'=>$team2GamesLost_doubles);
	
}

function countWonMatchesDoubles($round_scheduleID){
	$WonGames=Calculations::countWonGamesDoubles($round_scheduleID);
	$team1WonGames=$WonGames['team1GamesWon_doubles'];
	$team2WonGames=$WonGames['team2GamesWon_doubles'];
	$team1WonDoubles=0;
	$team2WonDoubles=0;
	$team1LostDoubles=0;
	$team2LostDoubles=0;
	if ($team1WonGames>$team2WonGames){
		$team1WonDoubles=2;
		$team2LostDoubles=2;
	}
	else if ($team2WonGames>$team1WonGames){
		$team2WonDoubles=2;
		$team1LostDoubles=2;
	}
	else if (($team2WonGames==$team1WonGames)&& ($team1WonGames!=0)){
		$doublesPoints=Calculations::countDoublesPoints($round_scheduleID);
		$team1Points=$doublesPoints[0];
		$team2Points=$doublesPoints[1];
		if ($team1Points>$team2Points){
			$team1WonDoubles=2;
			$team2LostDoubles=2;
		}
		else if ($team2Points>$team1Points)
		{
			$team2WonDoubles=2;
			$team1LostDoubles=2;
		}
		else if ($team2Points==$team1Points){
			$team1WonDoubles=1;
			$team1LostDoubles=1;
			$team2WonDoubles=1;
			$team2LostDoubles=1;
		}
	}
	
	$doublesMatches=array();
	
	$doublesMatches['team1WonDoubles']=$team1WonDoubles;
	$doublesMatches['team2WonDoubles']=$team2WonDoubles;
	$doublesMatches['team1LostDoubles']=$team1LostDoubles;
	$doublesMatches['team2LostDoubles']=$team2LostDoubles;
	return $doublesMatches;
}

function standinsTeam($divisionID,$firstRoundID,$lastRoundID ){
	$result=array();
	$repTeam = new TeamRepository;
	$repSchedule = new RoundScheduleRepository;
	$repMatch = new MatchRepository;
	$teams=$repTeam->maps($repTeam->getActiveTeams($divisionID));
	foreach($teams as $t){
		$schedules=$repSchedule->maps($repSchedule->getSchedulesBeforeRound($firstRoundID, $lastRoundID, $t->teamID));
		$totalWinMatches=0;
		$totalLostMatches=0;
		$totalgameWin=0;
		$totalgameLost=0;
		$roundsWon=0;
		$roundsLost=0;
		foreach ($schedules as $s){
			$winLostTeamMatches=Calculations::countTeamWonMatches($s->round_scheduleID);
			$winLostTeamDoublesGames=Calculations::countWonGamesDoubles($s->round_scheduleID);
			if ($s->team1==$t->teamID){
				$totalWinMatches=$totalWinMatches+$winLostTeamMatches['winTeam1'];
				$totalLostMatches=$totalLostMatches+$winLostTeamMatches['lostTeam1'];
				if ($winLostTeamMatches['winTeam1']>$winLostTeamMatches['winTeam2']){
					$roundsWon=$roundsWon+1;}
				elseif ($winLostTeamMatches['winTeam1']<$winLostTeamMatches['winTeam2']){
					$roundsLost=$roundsLost+1;
				}
			}
			if ($s->team2==$t->teamID){
				$totalWinMatches=$totalWinMatches+$winLostTeamMatches['winTeam2'];
				$totalLostMatches=$totalLostMatches+$winLostTeamMatches['lostTeam2'];
				if ($winLostTeamMatches['winTeam2']>$winLostTeamMatches['winTeam1']){
					$roundsWon=$roundsWon+1;}
				elseif ($winLostTeamMatches['winTeam2']<$winLostTeamMatches['winTeam1']){
					$roundsLost=$roundsLost+1;
				}
			}
			$matches=$repMatch->maps($repMatch->getMatchByRoundScheduleID($s->round_scheduleID)); 
			foreach($matches as $m){
				$playersGames=Calculations::wonGamesByMatch($m->matchID);
				if ($s->team1==$t->teamID){
					$totalgameWin=$totalgameWin+$playersGames['player1Games'];
					$totalgameWin=$totalgameWin+$winLostTeamDoublesGames['team1GamesWon_doubles'];
					$totalgameLost=$totalgameLost+$playersGames['player1Games_lost'];
					$totalgameLost=$totalgameLost+$winLostTeamDoublesGames['team1GamesLost_doubles'];
				}
				if ($s->team2==$t->teamID){
					$totalgameWin=$totalgameWin+$playersGames['player2Games'];
					$totalgameWin=$totalgameWin+$winLostTeamDoublesGames['team2GamesWon_doubles'];
					$totalgameLost=$totalgameLost+$playersGames['player2Games_lost'];
					$totalgameLost=$totalgameLost+$winLostTeamDoublesGames['team2GamesLost_doubles'];
				}
					
					

			}
		}
					$result[]=array('team'=>$t, 'roundsWon'=>$roundsWon, 'roundsLost'=>$roundsLost,'totalWinMatches'=>$totalWinMatches, 'totalLostMatches'=>$totalLostMatches, 'totalgameWin'=>$totalgameWin, 'totalgameLost'=>$totalgameLost);
					
	}
	foreach ($result as $key => $row) {
    $byGame[$key]  = $row['totalgameWin'];
    $byMatches[$key] = $row['totalWinMatches'];
	$byRounds[$key] = $row['roundsWon'];
	$byGameLost[$key] = $row['totalgameLost'];
	}

array_multisort($byRounds, SORT_DESC, $byMatches, SORT_DESC, $byGame, SORT_DESC, $byGameLost, SORT_ASC,  $result);
	

	return $result;
}

function standinsPlayer($divisionID,$firstRoundID,$lastRoundID ){
	$result=array();
	$regularPlayersWon=array();
	$regularPlayersLost=array();
	$fillinPlayersWon = array();
	$fillinPlayersLost = array();
	$repTeam = new TeamRepository;
	$repSchedule = new RoundScheduleRepository;
	$repMatch = new MatchRepository;
	$repPlayer=new PlayerRepository;
	$matchesPlayed=$repMatch->maps($repMatch->getMatchByRoundsDivisions($firstRoundID, $lastRoundID, $divisionID));
	
	$playersID_fact= $repPlayer->getPlayersByRounds($firstRoundID,$lastRoundID,$divisionID);

	foreach($matchesPlayed as $m){
		$round_scheduleID=$m->round_scheduleID;
		$schadule=$repSchedule->map($repSchedule->readRoundSchedule($round_scheduleID));
		$team1Players=$repTeam->getPlayerByTeamID($schadule->team1);
		$team2Players=$repTeam->getPlayerByTeamID($schadule->team2);
		$playersMatch=Calculations::wonMatch($m->matchID);
		if (($m->player1ID==$team1Players[0])||($m->player1ID==$team1Players[1])||($m->player1ID==$team1Players[2]))
		{
			
			$regularPlayersWon[]= array('playerID'=>$m->player1ID,'Won'=>$playersMatch['player1Match']);
			$regularPlayersLost[]=array('playerID'=>$m->player1ID,'Lost'=>$playersMatch['player1Match_lost']);	
		}
		else 
		{
			
			$fillinPlayersWon[]= array('playerID'=>$m->player1ID,'Won'=>$playersMatch['player1Match']);
			$fillinPlayersLost[]=array('playerID'=>$m->player1ID,'Lost'=>$playersMatch['player1Match_lost']);	
		}
		
		if (($m->player2ID==$team2Players[0])||($m->player2ID==$team2Players[1])||($m->player2ID==$team2Players[2]))
		{
			$regularPlayersWon[]= array('playerID'=>$m->player2ID,'Won'=>$playersMatch['player2Match']);
			$regularPlayersLost[]=array('playerID'=>$m->player2ID,'Lost'=>$playersMatch['player2Match_lost']);	
		}
		else 
		{
			$fillinPlayersWon[]= array('playerID'=>$m->player2ID,'Won'=>$playersMatch['player2Match']);
			$fillinPlayersLost[]=array('playerID'=>$m->player2ID,'Lost'=>$playersMatch['player2Match_lost']);
		}		
		
	}
	
	$regularPlayersStandigs=array();
	$fillinPlayersStandigs=array();
	foreach ($playersID_fact as $p){

		$wonMatches=0;
		$lostMatches=0;
		foreach ($regularPlayersWon as $w){
			if ($w['playerID']==$p){
				$wonMatches=$wonMatches+$w['Won'];
				
			}
		}
		foreach ($regularPlayersLost as $l){
			if ($l['playerID']==$p){
				$lostMatches=$lostMatches+$l['Lost'];
			}
		}
		foreach ($regularPlayersWon as $w){
			if ($w['playerID']==$p){
		$regularPlayersStandigs[]=array('playerID'=>$p,'TeamID'=>$repTeam->getTeamIDByPlayer($p),'Won'=>$wonMatches,'Lost'=>$lostMatches,'Rank'=>0);
				break; 
			}
		}
		
	}
	
	foreach ($playersID_fact as $p){

		$wonMatches=0;
		$lostMatches=0;
		foreach ($fillinPlayersWon as $w){
			if ($w['playerID']==$p){
				$wonMatches=$wonMatches+$w['Won'];
				
			}
		}
		foreach ($fillinPlayersLost as $l){
			if ($l['playerID']==118)
			{continue;}
			if ($l['playerID']==$p){
				$lostMatches=$lostMatches+$l['Lost'];
			}
		}
		foreach ($fillinPlayersLost as $l){
			if ($l['playerID']==118)
			{continue;;}
			if ($l['playerID']==$p){
				$fillinPlayersStandigs[]=array('playerID'=>$p,'TeamID'=>0,'Won'=>$wonMatches,'Lost'=>$lostMatches,'Rank'=>0);
				break; 
			}
		}
		
		
	}
	
	$overallStandings_sort = array();
		foreach ($regularPlayersStandigs  as $row){		
			$overallStandings_sort[]=$row;
		}
		foreach ($fillinPlayersStandigs as $row){
			$overallStandings_sort[]=$row;
		}
		foreach ($overallStandings_sort as $key=>$row){
			$playerID[$key]  = $row['playerID'];
			$TeamID[$key] = $row['TeamID'];
			$Won[$key] = $row['Won'];
			$Lost[$key] = $row['Lost'];
			$Rank[$key] = $row['Rank'];
		}
		
	
	array_multisort($Won, SORT_DESC, $Lost, SORT_ASC, $overallStandings_sort);
		$previousRank=0;
		$tr=0;
		$overallStandings=array();
	foreach ($overallStandings_sort as $s){
		
		if ($previousRank==0){
			
			$s['Rank']=1;
			$previousRank=$s['Rank'];
			$previousWon=$s['Won'];
			$previousLost=$s['Lost'];
		}
		else {
			if (($previousWon==$s['Won'])&&($previousLost==$s['Lost']))
			{
				$s['Rank']=$previousRank;
				$tr=$tr+1;
			}
			else
			{			
				$s['Rank']=$previousRank+1+$tr;
				$tr=0;
			}
			$previousRank=$s['Rank'];
			$previousWon=$s['Won'];
			$previousLost=$s['Lost'];
		}
		$overallStandings[]=$s;
	}
return $overallStandings;

}
}
?>